package com.mybook.allbook.controller;


import com.mybook.allbook.entity.User;
import com.mybook.allbook.service.UserService;
import com.mybook.allbook.utils.ValidateImageCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //登录方法
    @PostMapping("/login")
    public String login(String username, String password) {
        User login = userService.login(username, password);
        if (login != null) {
            return "redirect:/emp/findAll";//跳转到查询所有
        } else {
            return "redirect:/index";//跳转到回到登陆的控制器
        }
    }

    //注册方法
    @PostMapping("/register")
    public String register(User user, String code, HttpSession session) {
        String sessionCode = (String) session.getAttribute("code");
        if (sessionCode.equalsIgnoreCase(code)) {
            userService.register(user);
            return "redirect:/index";
        } else {
            return "redirect:/toRegister";
        }

    }


    //生成验证码
    @GetMapping("code")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String securityCode = ValidateImageCodeUtils.getSecurityCode();
        BufferedImage image = ValidateImageCodeUtils.createImage(securityCode);
        session.setAttribute("code", securityCode);//存入session作用于中
        //响应图片
        ServletOutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
    }


    //登录方法2
    @PostMapping("/login2")
    public String login2(String username, String password) {
        User login = userService.login(username, password);
        if (login != null) {
            return "redirect:/emp/findAll2";//跳转到查询所有
        } else {
            return "redirect:/index";//跳转到回到登陆的控制器
        }
    }

    @PostMapping("/login3")
    public String login3(String username, String password) {
        User login = userService.login(username, password);
        if (login != null) {
            return "redirect:/emp/findAll3";//跳转到查询所有
        } else {
            return "redirect:/index";//跳转到查询所有

        }
    }

    @PostMapping("/login4")
    public String login4(String username, String password) {
        User login = userService.login(username, password);
        if (login != null) {
            return "redirect:/zhuye";//跳转到查询所有
        } else {
            return "redirect:/index2";//跳转到查询所有

        }
    }
}